# This file contains all CREATE TABLE queries, used to sync and test schema
import re

from posthog.clickhouse.dead_letter_queue import (
    DEAD_LETTER_QUEUE_TABLE_MV_SQL,
    DEAD_LETTER_QUEUE_TABLE_SQL,
    KAFKA_DEAD_LETTER_QUEUE_TABLE_SQL,
    WRITABLE_DEAD_LETTER_QUEUE_TABLE_SQL,
)
from posthog.clickhouse.distributed_system_processes import DISTRIBUTED_SYSTEM_PROCESSES_TABLE_SQL
from posthog.clickhouse.log_entries import KAFKA_LOG_ENTRIES_TABLE_SQL, LOG_ENTRIES_TABLE_MV_SQL, LOG_ENTRIES_TABLE_SQL
from posthog.clickhouse.plugin_log_entries import (
    KAFKA_PLUGIN_LOG_ENTRIES_TABLE_SQL,
    PLUGIN_LOG_ENTRIES_TABLE_MV_SQL,
    PLUGIN_LOG_ENTRIES_TABLE_SQL,
    PLUGIN_LOG_ENTRIES_WRITABLE_TABLE_SQL,
)
from posthog.clickhouse.query_log_archive import (
    QUERY_LOG_ARCHIVE_DATA_TABLE,
    QUERY_LOG_ARCHIVE_MV,
    QUERY_LOG_ARCHIVE_NEW_MV_SQL,
    QUERY_LOG_ARCHIVE_NEW_TABLE_SQL,
    QUERY_LOG_ARCHIVE_TABLE_ENGINE_NEW,
)
from posthog.heatmaps.sql import (
    DISTRIBUTED_HEATMAPS_TABLE_SQL,
    HEATMAPS_TABLE_MV_SQL,
    HEATMAPS_TABLE_SQL,
    KAFKA_HEATMAPS_TABLE_SQL,
    WRITABLE_HEATMAPS_TABLE_SQL,
)
from posthog.models.ai.pg_embeddings import PG_EMBEDDINGS_TABLE_SQL
from posthog.models.app_metrics.sql import (
    APP_METRICS_DATA_TABLE_SQL,
    APP_METRICS_MV_TABLE_SQL,
    DISTRIBUTED_APP_METRICS_TABLE_SQL,
    KAFKA_APP_METRICS_TABLE_SQL,
    WRITABLE_APP_METRICS_TABLE_SQL,
)
from posthog.models.app_metrics2.sql import (
    APP_METRICS2_DATA_TABLE_SQL,
    APP_METRICS2_MV_TABLE_SQL,
    DISTRIBUTED_APP_METRICS2_TABLE_SQL,
    KAFKA_APP_METRICS2_TABLE_SQL,
    WRITABLE_APP_METRICS2_TABLE_SQL,
)
from posthog.models.channel_type.sql import (
    CHANNEL_DEFINITION_DATA_SQL,
    CHANNEL_DEFINITION_DICTIONARY_SQL,
    CHANNEL_DEFINITION_TABLE_SQL,
)
from posthog.models.cohort.sql import CREATE_COHORTPEOPLE_TABLE_SQL
from posthog.models.cohortmembership.sql import (
    COHORT_MEMBERSHIP_MV_SQL,
    COHORT_MEMBERSHIP_TABLE_SQL,
    COHORT_MEMBERSHIP_WRITABLE_TABLE_SQL,
    KAFKA_COHORT_MEMBERSHIP_TABLE_SQL,
)
from posthog.models.event.sql import (
    DISTRIBUTED_EVENTS_RECENT_TABLE_SQL,
    DISTRIBUTED_EVENTS_TABLE_SQL,
    EVENTS_RECENT_TABLE_JSON_MV_SQL,
    EVENTS_RECENT_TABLE_SQL,
    EVENTS_TABLE_JSON_MV_SQL,
    EVENTS_TABLE_SQL,
    KAFKA_EVENTS_RECENT_TABLE_JSON_SQL,
    KAFKA_EVENTS_TABLE_JSON_SQL,
    WRITABLE_EVENTS_RECENT_TABLE_SQL,
    WRITABLE_EVENTS_TABLE_SQL,
)
from posthog.models.exchange_rate.sql import (
    EXCHANGE_RATE_DATA_BACKFILL_SQL,
    EXCHANGE_RATE_DICTIONARY_SQL,
    EXCHANGE_RATE_TABLE_SQL,
)
from posthog.models.group.sql import (
    GROUPS_TABLE_MV_SQL,
    GROUPS_TABLE_SQL,
    GROUPS_WRITABLE_TABLE_SQL,
    KAFKA_GROUPS_TABLE_SQL,
)
from posthog.models.ingestion_warnings.sql import (
    DISTRIBUTED_INGESTION_WARNINGS_TABLE_SQL,
    INGESTION_WARNINGS_DATA_TABLE_SQL,
    INGESTION_WARNINGS_MV_TABLE_SQL,
    KAFKA_INGESTION_WARNINGS_TABLE_SQL,
    WRITABLE_INGESTION_WARNINGS_TABLE_SQL,
)
from posthog.models.performance.sql import (
    DISTRIBUTED_PERFORMANCE_EVENTS_TABLE_SQL,
    KAFKA_PERFORMANCE_EVENTS_TABLE_SQL,
    PERFORMANCE_EVENTS_TABLE_MV_SQL,
    PERFORMANCE_EVENTS_TABLE_SQL,
    WRITABLE_PERFORMANCE_EVENTS_TABLE_SQL,
)
from posthog.models.person.sql import (
    KAFKA_PERSON_DISTINCT_ID2_TABLE_SQL,
    KAFKA_PERSON_DISTINCT_ID_OVERRIDES_TABLE_SQL,
    KAFKA_PERSONS_DISTINCT_ID_TABLE_SQL,
    KAFKA_PERSONS_TABLE_SQL,
    PERSON_DISTINCT_ID2_MV_SQL,
    PERSON_DISTINCT_ID2_TABLE_SQL,
    PERSON_DISTINCT_ID2_WRITABLE_TABLE_SQL,
    PERSON_DISTINCT_ID_OVERRIDES_MV_SQL,
    PERSON_DISTINCT_ID_OVERRIDES_TABLE_SQL,
    PERSON_DISTINCT_ID_OVERRIDES_WRITABLE_TABLE_SQL,
    PERSON_STATIC_COHORT_TABLE_SQL,
    PERSONS_DISTINCT_ID_TABLE_MV_SQL,
    PERSONS_DISTINCT_ID_TABLE_SQL,
    PERSONS_TABLE_MV_SQL,
    PERSONS_TABLE_SQL,
    PERSONS_WRITABLE_TABLE_SQL,
)
from posthog.models.person_overrides.sql import (
    KAFKA_PERSON_OVERRIDES_TABLE_SQL,
    PERSON_OVERRIDES_CREATE_DICTIONARY_SQL,
    PERSON_OVERRIDES_CREATE_MATERIALIZED_VIEW_SQL,
    PERSON_OVERRIDES_CREATE_TABLE_SQL,
)
from posthog.models.precalculated_events.sql import (
    KAFKA_PRECALCULATED_EVENTS_TABLE_SQL,
    PRECALCULATED_EVENTS_MV_SQL,
    PRECALCULATED_EVENTS_SHARDED_TABLE_SQL,
    PRECALCULATED_EVENTS_WRITABLE_TABLE_SQL,
)
from posthog.models.raw_sessions.sessions_v2 import (
    DISTRIBUTED_RAW_SESSIONS_TABLE_SQL,
    RAW_SESSIONS_CREATE_OR_REPLACE_VIEW_SQL,
    RAW_SESSIONS_TABLE_MV_SQL,
    RAW_SESSIONS_TABLE_SQL,
    WRITABLE_RAW_SESSIONS_TABLE_SQL,
)
from posthog.models.raw_sessions.sessions_v3 import (
    DISTRIBUTED_RAW_SESSIONS_TABLE_SQL_V3,
    RAW_SESSIONS_CREATE_OR_REPLACE_VIEW_SQL_V3,
    RAW_SESSIONS_TABLE_MV_RECORDINGS_SQL_V3,
    RAW_SESSIONS_TABLE_MV_SQL_V3,
    SHARDED_RAW_SESSIONS_TABLE_SQL_V3,
    WRITABLE_RAW_SESSIONS_TABLE_SQL_V3,
)
from posthog.models.sessions.sql import (
    DISTRIBUTED_SESSIONS_TABLE_SQL,
    SESSIONS_TABLE_MV_SQL,
    SESSIONS_TABLE_SQL,
    SESSIONS_VIEW_SQL,
    WRITABLE_SESSIONS_TABLE_SQL,
)
from posthog.models.web_preaggregated.sql import (
    WEB_BOUNCES_COMBINED_VIEW_SQL,
    WEB_BOUNCES_DAILY_SQL,
    WEB_BOUNCES_HOURLY_SQL,
    WEB_BOUNCES_SQL,
    WEB_STATS_COMBINED_VIEW_SQL,
    WEB_STATS_DAILY_SQL,
    WEB_STATS_HOURLY_SQL,
    WEB_STATS_SQL,
)
from posthog.session_recordings.sql.session_recording_event_sql import (
    DISTRIBUTED_SESSION_RECORDING_EVENTS_TABLE_SQL,
    KAFKA_SESSION_RECORDING_EVENTS_TABLE_SQL,
    SESSION_RECORDING_EVENTS_TABLE_MV_SQL,
    SESSION_RECORDING_EVENTS_TABLE_SQL,
    WRITABLE_SESSION_RECORDING_EVENTS_TABLE_SQL,
)
from posthog.session_recordings.sql.session_replay_event_sql import (
    DISTRIBUTED_SESSION_REPLAY_EVENTS_TABLE_SQL,
    KAFKA_SESSION_REPLAY_EVENTS_TABLE_SQL,
    SESSION_REPLAY_EVENTS_TABLE_MV_SQL,
    SESSION_REPLAY_EVENTS_TABLE_SQL,
    WRITABLE_SESSION_REPLAY_EVENTS_TABLE_SQL,
)

from products.error_tracking.backend.embedding import (
    DISTRIBUTED_DOCUMENT_EMBEDDINGS_TABLE_SQL,
    DOCUMENT_EMBEDDINGS_MV_SQL,
    DOCUMENT_EMBEDDINGS_TABLE_SQL,
    DOCUMENT_EMBEDDINGS_WRITABLE_TABLE_SQL,
    KAFKA_DOCUMENT_EMBEDDINGS_TABLE_SQL,
)
from products.error_tracking.backend.sql import (
    ERROR_TRACKING_FINGERPRINT_EMBEDDINGS_MV_SQL,
    ERROR_TRACKING_FINGERPRINT_EMBEDDINGS_TABLE_SQL,
    ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES_MV_SQL,
    ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES_TABLE_SQL,
    KAFKA_ERROR_TRACKING_FINGERPRINT_EMBEDDINGS_TABLE_SQL,
    KAFKA_ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES_TABLE_SQL,
    WRITABLE_ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES_TABLE_SQL,
)

# Queries to create tables, you must pass function, otherwise the table is created before
# objects are mocked and the ambr will go into infinite loop update.
CREATE_MERGETREE_TABLE_QUERIES = (
    LOG_ENTRIES_TABLE_SQL,
    CREATE_COHORTPEOPLE_TABLE_SQL,
    PERSON_STATIC_COHORT_TABLE_SQL,
    DEAD_LETTER_QUEUE_TABLE_SQL,
    EVENTS_TABLE_SQL,
    EVENTS_RECENT_TABLE_SQL,
    GROUPS_TABLE_SQL,
    PERSONS_TABLE_SQL,
    PERSON_OVERRIDES_CREATE_TABLE_SQL,
    PERSONS_DISTINCT_ID_TABLE_SQL,
    PERSON_DISTINCT_ID2_TABLE_SQL,
    PERSON_DISTINCT_ID_OVERRIDES_TABLE_SQL,
    DOCUMENT_EMBEDDINGS_TABLE_SQL,
    ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES_TABLE_SQL,
    ERROR_TRACKING_FINGERPRINT_EMBEDDINGS_TABLE_SQL,
    PLUGIN_LOG_ENTRIES_TABLE_SQL,
    SESSION_RECORDING_EVENTS_TABLE_SQL,
    INGESTION_WARNINGS_DATA_TABLE_SQL,
    APP_METRICS_DATA_TABLE_SQL,
    APP_METRICS2_DATA_TABLE_SQL,
    PERFORMANCE_EVENTS_TABLE_SQL,
    SESSION_REPLAY_EVENTS_TABLE_SQL,
    WRITABLE_SESSION_REPLAY_EVENTS_TABLE_SQL,
    CHANNEL_DEFINITION_TABLE_SQL,
    EXCHANGE_RATE_TABLE_SQL,
    SESSIONS_TABLE_SQL,
    RAW_SESSIONS_TABLE_SQL,
    SHARDED_RAW_SESSIONS_TABLE_SQL_V3,
    HEATMAPS_TABLE_SQL,
    PG_EMBEDDINGS_TABLE_SQL,
    WEB_STATS_DAILY_SQL,
    WEB_BOUNCES_DAILY_SQL,
    WEB_STATS_HOURLY_SQL,
    WEB_BOUNCES_HOURLY_SQL,
    WEB_STATS_SQL,
    WEB_BOUNCES_SQL,
    lambda: QUERY_LOG_ARCHIVE_NEW_TABLE_SQL(
        table_name=QUERY_LOG_ARCHIVE_DATA_TABLE, engine=QUERY_LOG_ARCHIVE_TABLE_ENGINE_NEW()
    ),
    COHORT_MEMBERSHIP_TABLE_SQL,
    PRECALCULATED_EVENTS_SHARDED_TABLE_SQL,
)
CREATE_DISTRIBUTED_TABLE_QUERIES = (
    WRITABLE_EVENTS_TABLE_SQL,
    DISTRIBUTED_EVENTS_TABLE_SQL,
    DISTRIBUTED_EVENTS_RECENT_TABLE_SQL,
    WRITABLE_SESSION_RECORDING_EVENTS_TABLE_SQL,
    DISTRIBUTED_SESSION_RECORDING_EVENTS_TABLE_SQL,
    DISTRIBUTED_INGESTION_WARNINGS_TABLE_SQL,
    DISTRIBUTED_APP_METRICS_TABLE_SQL,
    DISTRIBUTED_APP_METRICS2_TABLE_SQL,
    WRITABLE_PERFORMANCE_EVENTS_TABLE_SQL,
    DISTRIBUTED_PERFORMANCE_EVENTS_TABLE_SQL,
    DISTRIBUTED_SESSION_REPLAY_EVENTS_TABLE_SQL,
    WRITABLE_SESSIONS_TABLE_SQL,
    WRITABLE_RAW_SESSIONS_TABLE_SQL,
    WRITABLE_RAW_SESSIONS_TABLE_SQL_V3,
    DISTRIBUTED_SESSIONS_TABLE_SQL,
    DISTRIBUTED_RAW_SESSIONS_TABLE_SQL,
    DISTRIBUTED_RAW_SESSIONS_TABLE_SQL_V3,
    WRITABLE_HEATMAPS_TABLE_SQL,
    DISTRIBUTED_HEATMAPS_TABLE_SQL,
    DISTRIBUTED_SYSTEM_PROCESSES_TABLE_SQL,
    COHORT_MEMBERSHIP_WRITABLE_TABLE_SQL,
    PRECALCULATED_EVENTS_WRITABLE_TABLE_SQL,
    GROUPS_WRITABLE_TABLE_SQL,
    PERSONS_WRITABLE_TABLE_SQL,
    PERSON_DISTINCT_ID2_WRITABLE_TABLE_SQL,
    PERSON_DISTINCT_ID_OVERRIDES_WRITABLE_TABLE_SQL,
    PLUGIN_LOG_ENTRIES_WRITABLE_TABLE_SQL,
    WRITABLE_DEAD_LETTER_QUEUE_TABLE_SQL,
    WRITABLE_INGESTION_WARNINGS_TABLE_SQL,
    WRITABLE_APP_METRICS_TABLE_SQL,
    WRITABLE_APP_METRICS2_TABLE_SQL,
    WRITABLE_ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES_TABLE_SQL,
    WRITABLE_EVENTS_RECENT_TABLE_SQL,
    DISTRIBUTED_DOCUMENT_EMBEDDINGS_TABLE_SQL,
    DOCUMENT_EMBEDDINGS_WRITABLE_TABLE_SQL,
)
CREATE_KAFKA_TABLE_QUERIES = (
    KAFKA_LOG_ENTRIES_TABLE_SQL,
    KAFKA_DEAD_LETTER_QUEUE_TABLE_SQL,
    KAFKA_EVENTS_TABLE_JSON_SQL,
    KAFKA_EVENTS_RECENT_TABLE_JSON_SQL,
    KAFKA_GROUPS_TABLE_SQL,
    KAFKA_PERSONS_TABLE_SQL,
    KAFKA_PERSON_OVERRIDES_TABLE_SQL,
    KAFKA_PERSONS_DISTINCT_ID_TABLE_SQL,
    KAFKA_PERSON_DISTINCT_ID2_TABLE_SQL,
    KAFKA_PERSON_DISTINCT_ID_OVERRIDES_TABLE_SQL,
    KAFKA_ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES_TABLE_SQL,
    KAFKA_ERROR_TRACKING_FINGERPRINT_EMBEDDINGS_TABLE_SQL,
    KAFKA_DOCUMENT_EMBEDDINGS_TABLE_SQL,
    KAFKA_PLUGIN_LOG_ENTRIES_TABLE_SQL,
    KAFKA_SESSION_RECORDING_EVENTS_TABLE_SQL,
    KAFKA_INGESTION_WARNINGS_TABLE_SQL,
    KAFKA_APP_METRICS_TABLE_SQL,
    KAFKA_APP_METRICS2_TABLE_SQL,
    KAFKA_PERFORMANCE_EVENTS_TABLE_SQL,
    KAFKA_SESSION_REPLAY_EVENTS_TABLE_SQL,
    KAFKA_HEATMAPS_TABLE_SQL,
    KAFKA_PRECALCULATED_EVENTS_TABLE_SQL,
    KAFKA_COHORT_MEMBERSHIP_TABLE_SQL,
)
CREATE_MV_TABLE_QUERIES = (
    LOG_ENTRIES_TABLE_MV_SQL,
    DEAD_LETTER_QUEUE_TABLE_MV_SQL,
    EVENTS_TABLE_JSON_MV_SQL,
    EVENTS_RECENT_TABLE_JSON_MV_SQL,
    GROUPS_TABLE_MV_SQL,
    PERSONS_TABLE_MV_SQL,
    PERSON_OVERRIDES_CREATE_MATERIALIZED_VIEW_SQL,
    PERSONS_DISTINCT_ID_TABLE_MV_SQL,
    PERSON_DISTINCT_ID2_MV_SQL,
    PERSON_DISTINCT_ID_OVERRIDES_MV_SQL,
    ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES_MV_SQL,
    ERROR_TRACKING_FINGERPRINT_EMBEDDINGS_MV_SQL,
    DOCUMENT_EMBEDDINGS_MV_SQL,
    PLUGIN_LOG_ENTRIES_TABLE_MV_SQL,
    SESSION_RECORDING_EVENTS_TABLE_MV_SQL,
    INGESTION_WARNINGS_MV_TABLE_SQL,
    APP_METRICS_MV_TABLE_SQL,
    APP_METRICS2_MV_TABLE_SQL,
    PERFORMANCE_EVENTS_TABLE_MV_SQL,
    SESSION_REPLAY_EVENTS_TABLE_MV_SQL,
    SESSIONS_TABLE_MV_SQL,
    RAW_SESSIONS_TABLE_MV_SQL,
    RAW_SESSIONS_TABLE_MV_SQL_V3,
    RAW_SESSIONS_TABLE_MV_RECORDINGS_SQL_V3,
    HEATMAPS_TABLE_MV_SQL,
    QUERY_LOG_ARCHIVE_NEW_MV_SQL(view_name=QUERY_LOG_ARCHIVE_MV, dest_table=QUERY_LOG_ARCHIVE_DATA_TABLE),
    COHORT_MEMBERSHIP_MV_SQL,
    PRECALCULATED_EVENTS_MV_SQL,
)

CREATE_TABLE_QUERIES = (
    CREATE_MERGETREE_TABLE_QUERIES
    + CREATE_DISTRIBUTED_TABLE_QUERIES
    + CREATE_KAFKA_TABLE_QUERIES
    + CREATE_MV_TABLE_QUERIES
)

CREATE_DICTIONARY_QUERIES = (
    PERSON_OVERRIDES_CREATE_DICTIONARY_SQL,
    CHANNEL_DEFINITION_DICTIONARY_SQL,
    EXCHANGE_RATE_DICTIONARY_SQL,
)

CREATE_DATA_QUERIES = (CHANNEL_DEFINITION_DATA_SQL(), EXCHANGE_RATE_DATA_BACKFILL_SQL())

CREATE_VIEW_QUERIES = (
    SESSIONS_VIEW_SQL,
    RAW_SESSIONS_CREATE_OR_REPLACE_VIEW_SQL,
    RAW_SESSIONS_CREATE_OR_REPLACE_VIEW_SQL_V3,
    WEB_STATS_COMBINED_VIEW_SQL,
    WEB_BOUNCES_COMBINED_VIEW_SQL,
)


def build_query(query):
    return query if isinstance(query, str) else query()


def get_table_name(query):
    query = build_query(query)
    try:
        return re.findall(r"[\.\s]`?([a-z0-9_]+)`?\s+(?:ON CLUSTER '[a-z0-9_]+'\s+)?|\(", query)[0]
    except Exception:
        raise ValueError(f"No table name found in query {query}")
